﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     UserRoleRela业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class UserRoleRelaBLL
    {
        private Wsitech.Persistence.Interface.IUserRoleRelaDao dao = null;

        private UserRoleRelaBLL()
        {
            dao = Wsitech.Persistence.Dao.UserRoleRelaSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static UserRoleRelaBLL instance = null;
        private static object syncRoot = new object();
        public static UserRoleRelaBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new UserRoleRelaBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public UserRoleRela GetDetail(System.String relaid)
        {
            try
            {
                return dao.GetDetail(relaid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<UserRoleRela> GetList()
        {
            try
            {
                IList<UserRoleRela> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<UserRoleRela> GetListByParams(object TopCounts, UserRoleRela userrolerela)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<UserRoleRela> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="userrolerela"></param>
        /// <returns></returns>
        public object Add(UserRoleRela userrolerela)
        {
            try
            {
                return dao.Add(userrolerela);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userrolerela"></param>
        /// <returns></returns>
        public int Update(UserRoleRela userrolerela)
        {
            try
            {
                return dao.Update(userrolerela);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RelaID"></param>
        /// <returns></returns>
        public int Delete(System.String relaid)
        {
            try
            {
                return dao.Delete(relaid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion


        /// <summary>
        /// 添加角色和用户关系
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="p"></param>
        public void AddRela(string roleid, string userid)
        {
            AdminRole role = AdminRoleBLL.Instance.GetDetail(roleid);
            AdminUser user = AdminUserBLL.Instance.GetDetail(userid);
            if (role != null && user != null)
            {
                UserRoleRela rela = new UserRoleRela();
                rela.RelaID = Guid.NewGuid().ToString();
                rela.UserId = user.UserID;
                rela.RoleId = role.RoleID;

                Add(rela);
            }
        }



        /// <summary>
        /// 删除用户对应的所有角色关系
        /// </summary>
        /// <param name="userid"></param>
        public void DeleteRelaByUserId(string userid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("userid", userid);
            IList<UserRoleRela> list = dao.GetListByParams(ht);

            foreach (UserRoleRela rela in list)
            {
                dao.Delete(rela.RelaID);
            }
        }
    }
}
